// pages/my/order/order.js
var app = getApp();
var pageoffset;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlist: [],
    curpage:0,
    blankshow: false,
    isLast:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    pageoffset = 0;
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          listhgt: res.windowHeight
        });
      },
    });
    // this.loadOrderlist(pageoffset);
    this.loadToServer();
  },
  loadToServer: function () {
    var url = app.globalData.webUrl + 'Goods/getBuyList';

    var tdata = { page: this.data.curpage };

    app.loadDataFromServer(url, this.loadCallBack, tdata);

    app.UpdateList_fun('order', false);
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: res.msg,
        image: '../imgs/tips.png'
      });
      return;
    }

    var orderlist = res.result;
    for (var i = 0; i < orderlist.length; i++) {
      orderlist[i].icon = app.globalData.ImgwebUrl + orderlist[i].icon;
      orderlist[i].showbuytime = app.formatDateTime(orderlist[i].buydate);
      orderlist[i].spec = ['正品', '优质服务'];
    }

    this.data.isLast = true;
    if (orderlist.length == app.globalData.pageSize) {
      this.data.curpage += 1;
      this.data.isLast = false;
    }
    if (orderlist.length <= 0) {
      this.setData({
        blankshow: true
      });
    }
    else {
      this.setData({
        blankshow: false
      });
    }
    this.data.orderlist = this.data.orderlist.concat(orderlist);
    this.setData({
      orderlist: this.data.orderlist
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.getUpdateList('order')==true){
      this.loadToServer();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  loadMoreGoods: function () {
    // pageoffset++;
    // this.loadOrderlist(pageoffset);
    if (!this.data.isLast)
      this.loadToServer();
  },
  loadOrderlist: function (offset) {
    var orderlist = [];
    if (offset > 0) {
      orderlist = this.data.orderlist;
    }
    var orderinfo = {
      name: '密酿靓肌原浆发酵面膜贴',
      intro: '紧致肌肤 恢复弹性',
      price: '99.00',
      img: '../../imgs/projects/goods2.jpg',
      spec: ['正品', '优质服务'],
      buytime: '2018-05-02'
    };
    orderlist.push(orderinfo);
    orderlist.push(orderinfo);
    orderlist.push(orderinfo);
    this.setData({
      orderlist: orderlist
    });
  },
  showOrderCoupon:function(e){
    var buyid = e.currentTarget.dataset.buyid;
    wx.navigateTo({
      url: './orderdetail/couponlist?buyid=' + buyid,
    });
  }
})